﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;


namespace StressClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ClientViewModel _Model;
        private StressClient _Client;
        public MainWindow()
        {
            InitializeComponent();
            _Client = new StressClient();
            _Model = new ClientViewModel(_Client);
            this.DataContext = _Model;
            //this.Messages.SelectionChanged += Messages_SelectionChanged;
            ((INotifyCollectionChanged)Messages.Items).CollectionChanged += MainWindow_CollectionChanged;
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (_Client != null)
            {
                _Client.Dispose();
            }
            Application.Current.Shutdown();
        }
        void MainWindow_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (!(Messages.Items.IsEmpty))
            {
                Messages.ScrollIntoView(Messages.Items[Messages.Items.Count - 1]);
            }
        }

        void Messages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!(Messages.Items.IsEmpty))
            {
                Messages.ScrollIntoView(Messages.Items[Messages.Items.Count - 1]);
            }
        }        
    }
    
}
